﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Entities;

namespace DAL
{
    public class GiaoVienProvider
    {
        public static QLTTTHEntities context = new QLTTTHEntities();
        public static List<GiaoVienInfo> LoadGiaoVien_List()
        {
            var lst = new List<GiaoVienInfo>();
            var sql = (from nd in context.NguoiDungs
                       join gv in context.GiaoViens
                       on nd.MaNV equals gv.MaNguoiDung
                       select new
                       {
                           MaNV = nd.MaNV,
                           Username = nd.Username,
                           Password = nd.Password,
                           HoTen = nd.HoTen,
                           GioiTinh = nd.GioiTinh,
                           NgaySinh = nd.NgaySinh,
                           Email = nd.Email,
                           DiaChi = nd.DiaChi,
                           SoDT = nd.SoDT,
                           ChucVu = nd.ChucVu,
                           MaNhomND = nd.MaNhomND,
                           TrangThai = nd.TrangThai,
                           ChuyenMon = gv.ChuyenMon
                       }).ToList();
            foreach (var item in sql)
            {
                var gv = new GiaoVienInfo();
                gv.MaNV = item.MaNV;
                gv.Username = item.Username;
                gv.Password = item.Password;
                gv.HoTen = item.HoTen;
                gv.GioiTinh = item.GioiTinh;
                gv.NgaySinh = item.NgaySinh;
                gv.DiaChi = item.DiaChi;
                gv.SoDT = item.SoDT;
                gv.ChucVu = item.ChucVu;
                gv.MaNhom = item.MaNhomND;
                gv.TrangThai = item.TrangThai;
                gv.ChuyenMon = item.ChuyenMon;
                gv.Email = item.Email;

                lst.Add(gv);

            }
            return lst;
        }

        public static int ThemGiaoVien(GiaoVien gv)
        {
            int maND = -1;
            try
            {
                using (QLTTTHEntities context = new QLTTTHEntities())
                {
                    context.GiaoViens.Add(gv);
                    context.SaveChanges();
                    maND = gv.MaNguoiDung;
                }
            }
            catch
            {

            }
            return maND;
        }


        public static bool XoaGiaoVien(int maGV)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.GiaoViens
                                 where u.MaNguoiDung.Equals(maGV)
                                 select u).First();

                    context.GiaoViens.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CapNhatGiaoVien(GiaoVien gv)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.GiaoViens
                                 where u.MaNguoiDung.Equals(gv.MaNguoiDung)
                                 select u).First();
                    query.ChuyenMon = gv.ChuyenMon;
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
